package com.tigercar.servicemanagement.controller;

import com.tigercar.common.pojo.PageResult;
import com.tigercar.servicemanagement.pojo.Dept;
import com.tigercar.servicemanagement.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

@RestController
public class DeptController {
    @Autowired
    private DeptService deptService;

    /**
     * 分页查询部门信息
     * liangjunjie
     * @param curPage 当前页
     * @param pageSize 页大小
     * @param deptId 查询部门id
     * @param deptName 查询部门名称
     * @return 封装数据
     */
    @GetMapping("/system/department/list")
    public ResponseEntity<PageResult<Dept>> searchDeptList(
            @RequestParam(value = "curPage",defaultValue = "1")Long curPage,
            @RequestParam(value = "pageSize",defaultValue = "5")Long pageSize,
            @RequestParam(value = "id",required = false) Integer deptId,
            @RequestParam(value = "name",required = false) String deptName
    ){
        PageResult<Dept> pageResult = deptService.searchDeptList(curPage,pageSize,deptId,deptName);
        return ResponseEntity.ok(pageResult);
    }

    /**
     * 新增部门
     * liangjunjie
     * @param deptId 新增部门id
     * @param deptName 新增部门名称
     * @return 无
     */
    @PostMapping("/system/department/add")
    public ResponseEntity<Void> addDept(
            @RequestParam("id") Integer deptId,
            @RequestParam("name") String deptName
    ){
        deptService.addDept(deptId,deptName);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    /**
     * 修改部门
     * liangjunjie
     * @param deptId 修改部门id
     * @param deptName 修改部门名称
     * @return 无
     */
    @PutMapping("/system/department/update")
    public ResponseEntity<Void> updateDept(
            @RequestParam("id") Integer deptId,
            @RequestParam("name") String deptName
    ){
        deptService.updateDept(deptId,deptName);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    /**
     * 删除部门
     * liangjunjie
     * @param deptId 删除部门id
     * @return 无
     */
    @DeleteMapping("/system/department/delete")
    public ResponseEntity<Void> deleteDept(@RequestParam("id") Integer deptId){
        deptService.deleteDept(deptId);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
}
